
###############################################
#
#Compute the netprod2 variable
#IMPORTANT: 
# -DATA NEED TO BE SET BEFORE RUNNING THE FUNCTION
#
################################################

netprod2<-function(indi,g,y){

  a3<-subset(y, auth1!=indi & auth2!=indi & auth3!=indi) 
  neigh1<-neighborhood(g,1, nodes=indi) #obtaining the neighborhood of order 1
  neigh2<-neighborhood(g,2, nodes=indi) #obtaining the neighborhood of order 2
  nodes2<-setdiff(neigh2[[1]],neigh1[[1]]) #obtaining the nodes at distance 2

  n1<-length(neigh1[[1]])
  n2<-length(neigh2[[1]])
  if (n1==n2){
    netprod2<-0
  }
  else{ 
    #Obtaining the articles data for each neighbor of order 2#
    nod2 <- sapply(nodes2, function(v) list(subset(a3,
                           auth2==v|auth1==v|auth3==v)) )

    authm<-as.data.frame(do.call("rbind",nod2))
    authmu<-unique(authm)
    prod2<-sum(authmu$prod)

    nod2n <- sapply(neigh1[[1]], function(v) list(subset(authmu,
                           auth2==v|auth1==v|auth3==v)) )
    authmn<-as.data.frame(do.call("rbind",nod2n))
    authmnu<-unique(authmn)

    if (nrow(authmn)==0){
      prod2n1<-0}
    else{
      prod2n1<-sum(authmnu$prod)}

    netprod2<-(prod2-prod2n1) #total productivity without i
  }

  netprod2
}
